<?php
// +----------------------------------------------------------------------
// | QSCMS
// +----------------------------------------------------------------------
// | Copyright (c)  2025 https://www.qqss.net All rights reserved.
// +----------------------------------------------------------------------
// | Licensed QSCMS is not free software, commercial use must purchase official license from official website to avoid unnecessary legal disputes.
// +----------------------------------------------------------------------
// | Author: Contract 990504246@qq.com
// +----------------------------------------------------------------------

namespace app\adminapi\controller\user;

use app\adminapi\controller\Base;
use app\common\model\UserSignLog;
use app\common\model\User;

class Sign extends Base
{
    /**
     * Get sign-in record list
     */
    public function lists()
    {
        $user_id = $this->request->post('user_id', 0);
        $date    = $this->request->post('date', '');

        $where = [];
        if ($user_id) {
            $where[] = ['user_id', '=', $user_id];
        }
        if ($date) {
            $where[] = ['sign_date', '=', $date];
        }

        $signModel = new UserSignLog();

        $res = $signModel->where($where)
            ->with(['user'])
            ->order('id', 'desc')
            ->paginate($this->limit);

        return $this->success('', [
            'list'  => $res->items(),
            'total' => $res->total(),
        ]);
    }

    /**
     * Get sign-in rules
     */
    public function rules()
    {
        $rules = [
            'points'             => [
                1 => 5,  // Continuous sign-in for 1 day rewards 5 points
                2 => 6,  // Continuous sign-in for 2 days rewards 6 points
                3 => 7,  // Continuous sign-in for 3 days rewards 7 points
                4 => 8,  // Continuous sign-in for 4 days rewards 8 points
                5 => 9,  // Continuous sign-in for 5 days rewards 9 points
                6 => 10, // Continuous sign-in for 6 days rewards 10 points
                7 => 15, // Continuous sign-in for 7 days rewards 15 points
            ],
            'reset_days'         => 7, // Reset to day 1 after continuous sign-in for more than 7 days
            'max_points_per_day' => 15, // Maximum sign-in points per day
            'description'        => trans('continuous_sign_reward_description')
        ];

        return $this->success('', $rules);
    }

    /**
     * Get sign-in statistics
     */
    public function stats()
    {
        // Get sign-in statistics
        $stats = [
            'total_signs'         => UserSignLog::count(),
            'today_signs'         => UserSignLog::whereDay('sign_date')->count(),
            'yesterday_signs'     => UserSignLog::whereDay('sign_date', 'yesterday')->count(),
            'month_signs'         => UserSignLog::whereMonth('sign_date')->count(),
            'total_points'        => UserSignLog::sum('points'),
            'today_points'        => UserSignLog::whereDay('sign_date')->sum('points'),
            'month_points'        => UserSignLog::whereMonth('sign_date')->sum('points'),
            'max_continuous_days' => UserSignLog::max('continuous_days'),
            'avg_continuous_days' => UserSignLog::avg('continuous_days'),
        ];

        return $this->success('', $stats);
    }

    /**
     * Get user sign-in records
     */
    public function userLogs()
    {
        $user_id = $this->request->post('user_id', 0);
        if (!$user_id) {
            return $this->error(trans('parameter_error'));
        }

        // Check if user exists
        $user = User::find($user_id);
        if (!$user) {
            return $this->error(trans('member_not_exists'));
        }

        // Get user's sign-in records for this month
        $month = $this->request->post('month', date('Y-m'));
        $start = $month . '-01';
        $end   = date('Y-m-t', strtotime($start));

        $logs = UserSignLog::where('user_id', $user_id)
            ->where('sign_date', '>=', $start)
            ->where('sign_date', '<=', $end)
            ->field('id, user_id, sign_date, continuous_days, points')
            ->select();

        // Get user sign-in statistics
        $stats = [
            'total_days'          => UserSignLog::where('user_id', $user_id)->count(),
            'month_days'          => UserSignLog::where('user_id', $user_id)
                ->where('sign_date', '>=', $start)
                ->where('sign_date', '<=', $end)
                ->count(),
            'max_continuous_days' => UserSignLog::where('user_id', $user_id)->max('continuous_days'),
            'total_points'        => UserSignLog::where('user_id', $user_id)->sum('points'),
            'month_points'        => UserSignLog::where('user_id', $user_id)
                ->where('sign_date', '>=', $start)
                ->where('sign_date', '<=', $end)
                ->sum('points'),
            'is_signed_today'     => UserSignLog::where('user_id', $user_id)
                ->where('sign_date', date('Y-m-d'))
                ->count() > 0
        ];

        return $this->success('', [
            'logs'  => $logs,
            'stats' => $stats,
            'month' => $month
        ]);
    }
}